<?php
namespace App\Admin\Repositories\Shop;

use App\Services\AdminService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Repositories\Repository;
use Illuminate\Pagination\LengthAwarePaginator;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ShopUser extends Repository
{
    /**
     * 定义主键字段名称
     *
     * @return string
     */
    public function getPrimaryKeyColumn()
    {
        return 'id';
    }

    /**
     * 查询表格数据
     *
     * @param Grid\Model $model
     * @return LengthAwarePaginator
     */
    public function get(Grid\Model $model)
    {
        $page_no = $model->getCurrentPage();

        $page_size = $model->getPerPage();

        $sn = $model->filter()->input('_search_') ?? '';

        $res = AdminService::getUserList($page_no, $page_size, $sn);

        if(!$res['code']) {
            throw new NotFoundHttpException('数据不存在');
        }

        $data = $res['data'];

        return $model->makePaginator(
            $data['count'] ?? 0,
            $data['lists'] ?? []
        );
    }
}
